<template>
  <div class="swiper">
    <swiper
      :slidesPerView="2"
      :autoplay="{ delay: 300000, disableOnInteraction: false }"
      :navigation="true"
      :scrollbar="{ draggable: false }"
      :space-between="40"
      :modules="modules"
      :loop="true"
      @mouseenter="enter"
      @mouseleave="leave"
      @swiper="onSwiper"
      @slideChange="onSlideChange"
    >
      <swiper-slide
        @mouseover="mouseoverFn(index)"
        v-for="(item, index) in swiperData"
        :key="index"
        class="swiperItem"
        :style="{ background: item.type == 1 ? '#212627' : '', borderRadius: '20px' }"
      >
        <div class="slide">
          <div>
            <video class="video">
              <source :src="$t(`messages.home.videoList[${index}].video`)" type="video/mp4" />
            </video>
          </div>

          <div class="imgTitle">
            <el-tooltip class="box-item" effect="dark" placement="top">
              <template #content>
                <div class="tooltipContent">
                  {{ $t(`messages.home.videoList[${index}].title`) }}
                </div>
              </template>
              <div class="title2">
                <AutoEllipsis ref="AutoEllipsisRef" :start-ellipsis-line="1">
                  <div style="font-weight: bold">
                    {{ $t(`messages.home.videoList[${index}].title`) }}
                  </div>
                </AutoEllipsis>
              </div>
            </el-tooltip>
            <el-tooltip class="box-item" effect="dark" placement="top">
              <template #content>
                <div style="width: 500px">
                  {{ $t(`messages.home.videoList[${index}].detils`) }}
                </div>
              </template>
              <div class="size maring">
                <AutoEllipsis class="AutoEllipsis" :start-ellipsis-line="5">
                  {{ $t(`messages.home.videoList[${index}].detils`) }}
                </AutoEllipsis>
              </div>
            </el-tooltip>
          </div>
          <div class="Footbutton center" v-if="item.type == 1">
            <div class="button center" @click="lookVideoFn(item)">Enquiry →</div>
          </div>
        </div>
      </swiper-slide>
    </swiper>
    <el-dialog v-model="dialogVisibleVideo" width="800" :before-close="handleClose">
      <div style="" :key="visivbUrlKey">
        <video class="videoDialog" controls loop>
          <source :src="visivbUrl" type="video/mp4" />
          <!-- <source src="../../../assets/video/22.mp4" type="video/mp4" /> -->
        </video>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { AutoEllipsis } from "auto-ellipsis-text"
import { homeCaseVideo } from '@/http/api/home.js'
import Zh from '@/assets/i18n/langs/zh.js'
import En from '@/assets/i18n/langs/en.js'
import { useI18n } from "vue-i18n"
import { toRaw, ref, reactive, onMounted, nextTick } from 'vue'
import { Swiper, SwiperSlide } from 'swiper/vue'
import 'swiper/css'
import 'swiper/scss/navigation'
import 'swiper/scss/pagination'
import { Autoplay, Navigation, Scrollbar, A11y } from 'swiper/modules'
const props = defineProps()
let { locale } = useI18n()
let dialogVisibleVideo = ref(false)
let visivbUrl = ref('')
let modules = [Autoplay, Navigation, Scrollbar, A11y]
//定义swiperNew,目的获取非响应式swiper
let swiperNew = ''
let visivbUrlKey = ref(0)
//鼠标移入
const enter = (data) => {
  swiperNew.autoplay.stop()
}
//鼠标移出
const leave = () => {
  swiperNew.autoplay.start()
}
const onSwiper = (swiper) => {
  // console.log(swiper);

  swiperNew = toRaw(swiper) //拿到swiper对象再转换为非响应式
}
const mouseoverFn = (data) => {
  swiperData.value.forEach((item, index) => {
    if (index == data) {
      item.type = 1
    } else {
      item.type = 0
    }
  })
}
const onSlideChange = () => {
  // console.log("slide change");
}
const lookVideoFn = (data) => {
  if (locale.value == 'en') {
    visivbUrl.value = data.videoEn
  } else {
    visivbUrl.value = data.videoZh
  }
  visivbUrlKey.value++
  dialogVisibleVideo.value = true
}
let pageData = reactive({
  page: 1,
  size: 50
})
let swiperData = ref([])
const getData = async () => {
  let res = await homeCaseVideo(pageData)
  Zh.messages.home.videoList = res.res.map((item) => {
    return {
      title: item.titleZh,
      detils: item.detilsZh,
      video: item.videoZh
    }
  })
  En.messages.home.videoList = res.res.map((item) => {
    return {
      title: item.titleEn,
      detils: item.detilsEn,
      video: item.videoEn
    }
  })

  swiperData.value = res.res
}
let AutoEllipsisRef = ref(null)
onMounted(() => {
  console.log(AutoEllipsis)

  getData()
})
</script>
<style lang="scss" scoped>
.AutoEllipsis {
  word-break: break-word !important;
}
.tooltipContent {
  width: 500px;
}
.swiper {
  width: 100% !important;
  height: 100% !important;
  margin: 0;
  border-radius: 20px;
  img {
    width: 100%;
    height: 100%;
  }

  .swiperItem {
    height: 800px;
    width: 500px !important;
  }
}

::v-deep(.swiper-pagination-bullet) {
  //修改分页器圆点大小
  width: 8px;
  height: 8px;
  background-color: #fff;
}

.swiper-horizontal > .swiper-pagination-bullets .swiper-pagination-bullet,
::v-deep(.swiper-pagination-horizontal.swiper-pagination-bullets) .swiper-pagination-bullet {
  // 分页器远点之间的距离
  margin: 0 10px;
}

::v-deep(.swiper-button-prev),
::v-deep(.swiper-button-next) {
  color: black;
  width: 70px;
  height: 70px;
  background: #ffffff;
  border-radius: 35px;
  display: none;
}
.imgTitle {
  margin-top: 50px;
  .title {
    font-size: 32px;
    font-weight: bold;
    color: #ffdd00;
  }
  .size {
    width: 400px;
    color: #ffffff;
    white-space: pre-wrap; /* 保留空白符序列，但是当到达容器边界时进行换行 */
    overflow-wrap: break-word; /* 在长单词或URL地址内部进行换行 */
    word-break: break-all; /* 允许在任意字符间断开（用于CJK文字等） */
  }
}
.video {
  width: 400px;
  height: 230px;
  border-radius: 30px;
}
.Footbutton {
  margin-top: 50px;
  margin-bottom: 20px;
  .button {
    width: 210px;
    height: 65px;
    border: 1px solid #ffdd00;
    border-radius: 10px;
    color: #ffdd00;
    font-size: 18px;
  }
}
.maring {
  margin-top: 50px;
  height: 120px;
}
.slide {
  padding: 40px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}
.title2 {
  width: 400px;
  display: flex;
  justify-content: center;
}
.videoDialog {
  width: 100%;
  height: 100%;
}
</style>

